// pages/choiceGate/choiceGate.js
var QQMapWX = require("../../../sdk/qqmap-wx-jssdk.min.js");

var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {},
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        if (options.name) {
            that.setData({
                cityName: options.name
            });
        }
        that.getBase();
        that.getCurrentAddress();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2
                });
                wx.setNavigationBarTitle({
                    title: "选择" + that.data.leaderName
                });
            }
        });
    },
    //获取当前位置
    getCurrentAddress: function() {
        var that = this;
        that.setData({
            addressName: "定位中..."
        });
        common.getAppBase().then(() => {
            qqmapsdk = new QQMapWX({
                key: that.data.appBase.location_key
            });
        });
        wx.getLocation({
            type: "gcj02",
            isHighAccuracy: true,
            success: function(res) {
                console.log(res);
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                });
                if (!that.data.cityName) {
                    that.getAutoCity();
                } else {
                    that.chooseUniacid();
                }
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: res.latitude,
                        longitude: res.longitude
                    },
                    success: function(res) {
                        //获取当前地址成功
                        console.log("获取当前地址成功" + JSON.stringify(res));
                        that.setData({
                            addressName: res.result.address_component.province + res.result.address_component.city + res.result.address_component.district + res.result.address_component.street_number
                        });
                    },
                    fail: function(res) {
                        that.setData({
                            addressName: "定位失败,请重新定位..."
                        });
                        console.log("获取当前地址失败");
                    }
                });
            },
            fail: function(res) {
                console.log("获取定位失败");
                wx.showModal({
                    title: "提示",
                    content: "地理位置调用失败,是否重新授权!",
                    success(res) {
                        if (res.confirm) {
                            that.continueGetSet();
                        } else if (res.cancel) {
                            console.log("用户点击取消");
                        }
                    }
                });
                that.setData({
                    addressName: "定位失败,请重新定位..."
                });
            }
        });
    },
    continueGetSet: function() {
        let that = this;
        wx.getSetting({
            success(res) {
                console.log(res);
            }
        });
        wx.openSetting({
            success(res) {
                console.log(res);
                if (res.authSetting["scope.userLocation"]) {
                    that.getCurrentAddress();
                }
            }
        });
    },
    getAutoCity: function() {
        let that = this;
        let masterUniacid = wx.getStorageSync("uniacid");
        common.request({
            endpoint: "DIY/new-entry/auto-city",
            method: "post",
            data: {
                masterUniacid: masterUniacid,
                latitude: that.data.latitude,
                longitude: that.data.longitude
            },
            header: {},
            success: function(res) {
                that.setData({
                    cityInfo: res.extra
                });
                wx.setStorageSync("uniacid", res.extra.autoUniacid);
                that.chooseUniacid();
            }
        });
    },
    chooseUniacid: function() {
        let that = this;
        common.request({
            endpoint: "leader/near_leader",
            method: "post",
            data: {
                new_city_style: 1,
                //0 全部 1只搜索当前uniacid
                address: that.data.latitude + "," + that.data.longitude,
                openid: wx.getStorageSync("user").openid,
                key_word: that.data.search_con
            },
            header: {},
            success: function(res) {
                that.setData({
                    cityList: res.extra
                });
            }
        });
    },
    //选择团长
    setLeaderInfo: function(e) {
        let that = this;
        var id = e.currentTarget.dataset.id;
        var item = e.currentTarget.dataset.item;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "member/save_leader_id",
            method: "post",
            data: {
                openid: openid,
                leader_id: id
            },
            success: res => {
                that.setData({
                    currentLeader: item
                });
            },
            fail(err) {
                if (err.code == 1) {
                    wx.showToast({
                        title: "你是" + that.data.leaderName + ",无法切换" + that.data.leaderName + "!",
                        icon: "none"
                    });
                } else {
                    wx.showToast({
                        title: err.message,
                        icon: "none"
                    });
                }
            }
        });
    },
    //记录搜索名称
    saveWord: function(e) {
        var that = this;
        var search_con = e.detail.value;
        that.setData({
            search_con: search_con
        });
    },
    goToDetail(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    goBack: function(e) {
        wx.switchTab({
            url: e.currentTarget.dataset.url
        });
    }
});